package com.android.taobao.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import android.util.Log;

public class Util {

	/**
	 * 将URL中的参数字符串转成HashMap
	 * 
	 * @param parameters
	 * @return
	 */
	public static Map<String, String> handleURLParameters(String result) {
		String[] parameters = result.split("&");
		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < parameters.length; i++) {
			int num = parameters[i].trim().indexOf("=");
			String key = parameters[i].trim().substring(0, num);
			String value = parameters[i].trim().substring(num + 1);
			map.put(key, value);
		}
		return map;
	}

	/**
	 * BASE64解密
	 * 
	 * @param str
	 * @return
	 */
	public static String decodeBase64(String str) {
		String tag = "decodeBase64";
		String result = "";
		try {
			byte[] buf = new BASE64Decoder().decodeBuffer(str);
			result = new String(buf, "UTF-8");
		} catch (IOException e) {
			Log.i(tag, "Exception", e);
		}
		return result;
	}

	/**
	 * BASE64加密
	 * 
	 * @param str
	 * @return
	 */
	public static String encodeBase64(String str) {
		String tag = "encodeBase64";
		String result = "";
		try {
			byte[] buf = str.getBytes("UTF-8");
			result = new BASE64Encoder().encode(buf);
		} catch (IOException e) {
			Log.i(tag, "Exception", e);
		}
		return result;
	}
}